<template>
  <div id="updateMy">
    <el-dialog title="编辑个人信息" :visible.sync="visible" top="0.5rem" :lock-scroll="false" :show-close="false" :close-on-click-modal="false">
      <el-form ref="Myform" :model="item" :rules="rules" label-width="100px">
        <el-form-item>
          <div class="avatarlist" style="display:grid;justify-content:start">
            <el-upload
                class="avatar-uploader"
                name="img"
                :action="uploadURL"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img   v-if="item.avatar" :src="item.avatar?item.avatar:'../../../assets/img/bgImg/plant3.png'" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              <div slot="tip" class="el-upload__tip">点击上传头像，只能上传jpg/png文件，且不超过1mb</div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="用户名:" prop="userName">
          <el-input v-model="item.userName"  palceholder="请输入用户名" clearable/>
        </el-form-item>
        <el-form-item label="昵称:" prop="nickName">
          <el-input v-model="item.nickName"  palceholder="请输入昵称" clearable/>
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="item.email"  palceholder="请输入邮箱" clearable/>
        </el-form-item>
        <el-form-item label="性别："prop="sex" label-width="100px">
          <el-select v-model="item.sex" placeholder="性别" style="width: 100px;right: 200px">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
      <el-button type="primary" @click="resetForm('empForm')">取消</el-button>
      <el-button type="primary" @click="submitForm('empForm')">保存</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import store from "@/store";
export default {
  name: "UpdateMyInfo",
  data(){
    return {
      fileList: [],
      uploadURL:'',
      visible:false,
      item:{
        id:'',
        userName:'',
        nickName:'',
        avatar:'',
        sex:'',
        email:'',
        type:'',
        followerNum: ''
        //password:''
      },
      rules: {
        userName:[{required: true, message: '请输入用户名', trigger: 'change'}],
        nickName:[{required: true, message: '请输入用户名', trigger: 'change'}],
        sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
        email:[{ required: false, trigger: 'blur' }],
      }
    }
  },
  created() {
    this.uploadURL = store.state.baseURL+'upload'
    console.log(this.uploadURL)

  },
  methods:{
    handleRemove(file, fileList) {
      this.fileList = this.fileList.filter((val) => {
        (val.url).indexOf(file.url) < 0
      })
    },
    handleAvatarSuccess(res, file) {//上传头像
      console.log("hhhhh",res)

      if(res != null){
        this.item.avatar = res;
        //this.userInfoObj.head_start = 1;
      }else{
        this.$message.error('上传图片失败');
      }

    },
    beforeAvatarUpload(file) {//判断头像大小
      const isJPG = file.type == 'image/png'||file.type=='image/jpg'||file.type=='image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 1;
      // console.log(file);
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/JPEG/PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1MB!');
      }
      return isJPG && isLt2M;
    },
    open (item) {
      this.visible=true
      this.item.id=item.id
      this.item.userName=item.userName
      this.item.nickName=item.nickName
      this.item.sex=item.sex
      this.item.email=item.email
      this.item.avatar=item.avatar
      this.item.type=item.type
      this.item.followerNum=item.followerNum
      //let user=localStorage.getItem("userInfo")
      //this.item.password=user.password

    },
    routeChange:function(){

    },
    submitForm (roomtypeForm) {
      this.$refs.Myform.validate(valid => {
        if (valid) {
          this.$confirm('确认保存吗？', '是否保存', {
            cancelButtonText: '取消',
            confirmButtonText: '确认',
            lockScroll: false,
            type: 'warning'
          }).then(() => {
            console.log('this.fileList', this.fileList)
            this.$emit('confirmData', this.item,this.fileList)
            this.resetForm('，MyForm')
          })
        }
      })
    },
    resetForm (myForm) {
      this.$nextTick(() => {
        this.$refs.Myform.clearValidate()
      })
      this.item = {}
      this.visible = false
    }
  }

}
</script>

<style scoped>
.avatarlist{
  position: relative;
}
.leftTitle{
  display: inline-block;
  width:100px;
  padding: 10px 0;
}
</style>